# ==============
# Table 1: Summary of hearings, witnesses, agencies, and programs
# by fiscal year.
# ==============

  # All hearings mentioning IP.
  ALL = fread(file.path(DATALOC,"hearings_summary.csv"))
  setnames(ALL,c("fiscalyear"),c("FY"))
  # Create hearing identifier based on variables filename and title.
  ALL[,hearing_id := .GRP,by=c("filename","title")]

  # Aggregate to the fiscal year.
  ALL2 = ALL[,c(n_hearings=uniqueN(hearing_id),n_agencies=uniqueN(parent),lapply(.SD,sum)),keyby=c("FY"),.SDcols=patterns("num_witness_agency")]

  # Collapse pre-2000 hearings into one category.
  f = file.path(TABLELOC,"Table01.tex")
  ALL2[,"Fiscal year" := ifelse(FY < 2003,"1968 to 2002",paste(FY))]
  ALL22 = merge(ALL2[,lapply(.SD,sum),.SDcols=c("n_agencies", "num_witness_agency", "n_hearings"),by="Fiscal year"], ALL2[,.(FY=min(FY)),by="Fiscal year"],all.x=T)
  setkey(ALL22,FY)
  setnames(ALL22,c("n_agencies", "num_witness_agency", "n_hearings"),c("Agencies", "Witnesses", "Hearings"))
  # Two tables column-bound together.
  latex(cbind(ALL22[FY < 2012,c("Fiscal year", "Hearings", "Agencies", "Witnesses")], ALL22[FY >= 2012,c("Fiscal year", "Hearings", "Agencies", "Witnesses")]), table.env=FALSE, rowname=NULL, col.just=rep("c",8), numeric.dollar=FALSE, file=f)
  #system2("open",f)

